/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2017-08-24
 * 修改历史 :
 * 1. [2017-08-24]创建文件 by chenxin
 */
package com.gavinwind.inspay.api.paychannel.alipay.service.impl;

import com.gavinwind.inspay.api.paychannel.alipay.report.common.AllinpayPayHeader;
import com.gavinwind.inspay.api.paychannel.alipay.report.common.AllinpayPayRequestHeader;
import com.gavinwind.inspay.api.paychannel.alipay.report.payintime.AllinpayInTimeRequestBody;
import com.gavinwind.inspay.api.paychannel.alipay.report.payintime.AllinpayInTimeRequestXml;
import com.gavinwind.inspay.api.paychannel.apiservice.dto.PayInTimeReturnDTO;
import com.gavinwind.inspay.api.paychannel.apiservice.service.PayInTimeApiService;
import com.gavinwind.inspay.base.BaseService;
import com.gavinwind.inspay.common.model.PayRequestDTO;
import com.gavinwind.inspay.common.model.PayResponseDTO;
import com.gavinwind.inspay.common.util.DateUtil;
import com.gavinwind.inspay.common.util.XStreamComponent;

import java.util.Date;

/**
 * 【类或接口功能描述】
 * @author chenxin
 * @date 2017-08-24 上午9:56 
 * @version V1.0
 */
public class AliPayPayInTimeService extends BaseService implements PayInTimeApiService {
    public PayInTimeReturnDTO payInTime(PayRequestDTO payRequestDTO) {

        PayResponseDTO payInfoReturn = new PayResponseDTO();  //返回信息 给交易服务
        try{
            Integer tPayMoney = payRequestDTO.getPayMoney();

            AllinpayInTimeRequestXml requestXml = new  AllinpayInTimeRequestXml();
            AllinpayPayRequestHeader reqHeadInfo = new AllinpayPayRequestHeader();
            String tPath="/config/allinpayConfig.properties";
            Properties properties = PropertiesUtils.readProperties(tPath);
            reqHeadInfo.setDATA_TYPE(properties.getProperty("DATA_TYPE"));    //报文头 数据格式
            reqHeadInfo.setLEVEL(properties.getProperty("LEVEL"));            //处理级别
            Date currentDate = DateUtil.getCurrentDate();

            reqHeadInfo.setREQ_SN(properties.getProperty("REQ_SN")+currentDate.getTime());      //交易流水号 商户ID +系统时间
            reqHeadInfo.setTRX_CODE("100011");      //交易代码 properties.getProperty("TRX_CODE")
            reqHeadInfo.setMERCHANT_ID(properties.getProperty("MERCHANT_ID"));
            reqHeadInfo.setUSER_NAME(properties.getProperty("USER_NAME"));    //用户名
            reqHeadInfo.setUSER_PASS(properties.getProperty("USER_PASS"));    //用户密码
            reqHeadInfo.setVERSION(properties.getProperty("VERSION"));  //版本
            reqHeadInfo.setSIGNED_MSG("");   //签名信息
            requestXml.setINFO(reqHeadInfo);
            AllinpayInTimeRequestBody reqTransSum = new AllinpayInTimeRequestBody(); //报文体
            reqTransSum.setBUSINESS_CODE("10600");
            reqTransSum.setMERCHANT_ID(properties.getProperty("MERCHANT_ID"));
            //提交时间 为当前系统时间
            reqTransSum.setSUBMIT_TIME(interPayRequestDTO.getSubmitTime());
            reqTransSum.setBANK_CODE(interPayRequestDTO.getBankCode());
            reqTransSum.setACCOUNT_TYPE(properties.getProperty("ACCOUNT_TYPE"));
            reqTransSum.setACCOUNT_NO(interPayRequestDTO.getAccountNo());
            reqTransSum.setACCOUNT_NAME(interPayRequestDTO.getAccountName());
            reqTransSum.setACCOUNT_PROP(properties.getProperty("ACCOUNT_PROP"));
            reqTransSum.setAMOUNT(tPayMoney.toString());
            reqTransSum.setCURRENCY(properties.getProperty("CURRENCY"));
            requestXml.setTRANS(reqTransSum);
            //封装请求的xml
            XStreamComponent xstream = new XStreamComponent();
            xstream.processAnnotations(new Class[] { AllinpayInTimeRequestXml.class,AllinpayPayHeader.class,AllinpayInTimeRequestBody.class,
                    CommRealtimeCollectRespXml.class,CommRealtimeCollectRespHead.class,CommRealtimeCollectResp.class});
            String reqXml = properties.getProperty("Package_Header")+xstream.toXML(requestXml).replace("__", "_");
            System.out.println("请求报文：\n"+reqXml);
            String signData = this.signMsg(reqXml);
            String url = properties.getProperty("SERVER_URL");
            boolean flag =false;
            String strResp = sendXml(signData,url,flag);
            //解析返回报文
            CommRealtimeCollectRespXml response = (CommRealtimeCollectRespXml) xstream.fromXML(strResp);
            String returnCode = response.getINFO().getRET_CODE(); //头部返回编码
            if(returnCode!=null && !returnCode.equals("0000")){  //失败返回 信息处理
                payInfoReturn.setReturnCode(returnCode);
                payInfoReturn.setReturnMsg(response.getINFO().getERR_MSG());  //失败信息
            }else{  //成功 返回信息
                if(response.getTRANSRET().getRET_CODE().equals("0000")){
                    payInfoReturn.setReturnCode(response.getTRANSRET().getRET_CODE());  //返回编码
                    payInfoReturn.setReturnMsg(response.getTRANSRET().getERR_MSG());
                    payInfoReturn.setPayAmount(interPayRequestDTO.getAmount());
    		            	/*payInfoReturn.setPayNo(interPayRequestDTO.getPayNo());*/

                }else{
                    payInfoReturn.setReturnCode(response.getTRANSRET().getRET_CODE());
                    payInfoReturn.setReturnMsg(response.getTRANSRET().getERR_MSG());
                }
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            payInfoReturn.setReturnCode("FAIL");
            payInfoReturn.setReturnMsg("异常信息："+ex.getMessage());
        }
        return payInfoReturn;
    }

    public String getPayGatewayUrl(PayRequestDTO cRequestDTO) {
        return null;
    }
}
